//
//  fugitiveViewController.m
//  fugitive
//
//  Created by Eugene Bistolas on 5/25/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "fugitiveViewController.h"
#import "ServerEngine.h"

@implementation fugitiveViewController

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.



- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[self navigationController] setNavigationBarHidden:YES animated:NO];
    loc = [[CLLocationManager alloc] init]; 
    loc.desiredAccuracy = kCLLocationAccuracyBest; 
    loc.delegate = self; 
    [loc startUpdatingLocation];
    driverButton.hidden = YES;
    runnerButton.hidden = YES;
    observerButton.hidden = YES;
    typeImage.hidden = YES;
    backButton.hidden = YES; 
    [self hideSecondView]; 
    NSLog(@"Main view loaded"); 
}


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    MKCoordinateSpan span = MKCoordinateSpanMake(0.05, 0.05); 
    MKCoordinateRegion region = MKCoordinateRegionMake(newLocation.coordinate, span); 
    [titleMap setRegion:region animated:YES]; 
    [titleMap regionThatFits:region]; 
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"GPS Error (manifested in Simulator)\n");
}

- (void) viewWillAppear:(BOOL)animated {
    NSLog(@"Main view appearing"); 
    [super viewWillAppear:animated];
    [[self navigationController] setNavigationBarHidden:YES animated:NO];
    startButton.hidden = YES;
    joinButton.hidden = YES;
    helpButton.hidden = YES;
    titleImage.hidden = YES; 
    [self hideFirstView]; 
}

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated]; 
    startButton.hidden = NO;
    joinButton.hidden = NO;
    helpButton.hidden = NO;
    titleImage.hidden = NO; 
    
    [self restoreFirstView]; 
    
}

- (void) viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated]; 
    [[self navigationController] setNavigationBarHidden:YES animated:YES];
    
}

- (void)dealloc
{
    [startButton release];
    [joinButton release];
    [helpButton release];
    [titleMap release];
    [titleImage release];
    [driverButton release];
    [runnerButton release];
    [observerButton release];
    [typeImage release];
    [backButton release];
    [super dealloc];
}

- (IBAction) buttonPressed:(UIButton *)sender {
    
    switch (sender.tag) {
            //start
        case 1: {
            
            PlayerNameViewController *pnvc = [[PlayerNameViewController alloc] init]; 
            [self.navigationController presentModalViewController:pnvc animated:YES]; 
            [pnvc release]; 
            
            GameSetupViewController *gsvc = [[GameSetupViewController alloc] initWithNibName:nil bundle:nil]; 
            [self.navigationController pushViewController:gsvc animated:YES]; 
            [gsvc release]; 
            break;
        }
            //join
        case 2: {
            //Get instance here. 
            [self hideFirstView]; 
            driverButton.hidden = NO;
            runnerButton.hidden = NO;
            observerButton.hidden = NO;
            typeImage.hidden = NO; 
            backButton.hidden = NO; 
            GCPINViewController *gcpc = [[GCPINViewController alloc] initWithNibName:@"PINViewDefault" bundle:nil]; 
            gcpc.delegate = self; 
            [gcpc setMessageText:@"Enter Game Number You Wish To Join"];
            [gcpc setTitle:@"Join Game Number"]; 
            [self.navigationController presentModalViewController:gcpc animated:YES]; 
            break; 
        }
            //help
        case 3: {
            HelpViewController *hvc = [[HelpViewController alloc] init]; 
            [self presentModalViewController:hvc animated:YES];
            break;
        }
            //Driver
        case 4: {
            PlayerNameViewController *pnvc = [[PlayerNameViewController alloc] init]; 
            [self.navigationController presentModalViewController:pnvc animated:YES]; 
            [pnvc release]; 
            break;
        }
            //Runner
        case 5: {
            
            break;
        }
            //Observer
        case 6: {
            
            break;
        }
            //back button. 
        case 7: {
            [self hideSecondView]; 
            [self performSelector:@selector(restoreFirstView) withObject:nil afterDelay:0.4];
            break;
        }
            
        default:
            break;
    }
}

- (void) newNameData:(NSString *)name {
    NSLog(@"Recorded name: %@", name); 
}

- (void) pinViewClosing {
    [self performSelector:@selector(restoreSecondView) withObject:nil afterDelay:0.1];
}

- (BOOL)pinView:(GCPINViewController *)pinView validateCode:(NSString *)code {
    NSLog(@"Game Inst: %@\n", code); 
    [pinView dismissModalViewControllerAnimated:YES];
    return YES; 
}

- (void) restoreSecondView {
    [self animateButtonOrImage:IMAGE direction:DOWN tag:2 amount:200 speed:0.8];
    [self animateButtonOrImage:BUTTON direction:RIGHT tag:4 amount:350 speed:0.8];
    [self animateButtonOrImage:BUTTON direction:LEFT tag:5 amount:350 speed:0.8]; 
    [self animateButtonOrImage:BUTTON direction:RIGHT tag:6 amount:350 speed:0.8];
    [self animateButtonOrImage:BUTTON direction:LEFT tag:7 amount:350 speed:0.8];
    
}

- (void) hideSecondView {
    [self animateButtonOrImage:IMAGE direction:UP tag:2 amount:200 speed:0.8];
    [self animateButtonOrImage:BUTTON direction:LEFT tag:4 amount:350 speed:0.8];
    [self animateButtonOrImage:BUTTON direction:RIGHT tag:5 amount:350 speed:0.8]; 
    [self animateButtonOrImage:BUTTON direction:LEFT tag:6 amount:350 speed:0.8]; 
    [self animateButtonOrImage:BUTTON direction:RIGHT tag:7 amount:350 speed:0.8]; 
}

- (void) hideFirstView {
    
    [self animateButtonOrImage:BUTTON direction:LEFT tag:1 amount:350 speed:0.8];
    [self animateButtonOrImage:BUTTON direction:LEFT tag:2 amount:350 speed:0.8]; 
    [self animateButtonOrImage:BUTTON direction:DOWN tag:3 amount:350 speed:0.8]; 
    [self animateButtonOrImage:IMAGE direction:UP tag:1 amount:200 speed:0.8];
}

- (void) restoreFirstView {
    
    [self animateButtonOrImage:BUTTON direction:RIGHT tag:1 amount:350 speed:0.8];
    [self animateButtonOrImage:BUTTON direction:RIGHT tag:2 amount:350 speed:0.8]; 
    [self animateButtonOrImage:BUTTON direction:UP tag:3 amount:350 speed:0.8]; 
    [self animateButtonOrImage:IMAGE direction:DOWN tag:1 amount:200 speed:0.8];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


-(void)animateButtonOrImage:(int) type direction:(int) d tag:(int) t amount:(int) a speed:(double)s
{
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:s];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
	
	for (UIView *view in [self.view subviews])
	{
        if ([view isKindOfClass:[UIButton class]] && type == BUTTON) {
            CGRect frame = view.frame;
            if (((UIButton*)view).tag == t) {
                if (d == UP) {
                    frame.origin.y -= a; 
                }
                else if (d == DOWN) {
                    frame.origin.y += a;
                }
                else if (d == LEFT) {
                    frame.origin.x -= a;
                }
                else if (d == RIGHT) {
                    frame.origin.x += a; 
                }
            }
            view.frame = frame;
        }
        else if ([view isKindOfClass:[UIImageView class]] && type == IMAGE) {
            CGRect frame = view.frame;
            if (((UIButton*)view).tag == t) {
                if (d == UP) {
                    frame.origin.y -= a; 
                }
                else if (d == DOWN) {
                    frame.origin.y += a;
                }
                else if (d == LEFT) {
                    frame.origin.x -= a;
                }
                else if (d == RIGHT) {
                    frame.origin.x += a; 
                }
            }
            view.frame = frame;
        }
	}
	[UIView commitAnimations];
}

#pragma mark - View lifecycle




- (void)viewDidUnload
{
    [startButton release];
    startButton = nil;
    [joinButton release];
    joinButton = nil;
    [helpButton release];
    helpButton = nil;
    [titleMap release];
    titleMap = nil;
    [titleImage release];
    titleImage = nil;
    [driverButton release];
    driverButton = nil;
    [runnerButton release];
    runnerButton = nil;
    [observerButton release];
    observerButton = nil;
    [typeImage release];
    typeImage = nil;
    [backButton release];
    backButton = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


@end
